﻿namespace ChainOfResponsibility
{
    internal abstract class Support
    {
        private string name;
        private Support next;

        public Support(string name)
        {
            this.name = name;
        }

        public Support SetNext(Support next)
        {
            this.next = next;
            return this.next;
        }

        public void Execute(Trouble trouble)
        {
            if (this.Resolve(trouble))
            {
                this.Done(trouble);
            }
            else if (this.next is not null)
            {
                this.next.Execute(trouble);
            }
            else
            {
                this.Fail(trouble);
            }
        }

        public override string ToString()
        {
            return $"[{this.name}]";
        }

        protected abstract bool Resolve(Trouble trouble);

        protected void Done(Trouble trouble)
        {
            Console.WriteLine($"{trouble} is resolved by {this}.");
        }

        protected void Fail(Trouble trouble)
        {
            Console.WriteLine($"{trouble} cannot be resolved.");
        }
    }
}
